package com.qf.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fileUpload")
public class FileUploadController {


    @Value("${fdfs.server}")
    private String fdfsServer; // http://192.168.193.100/

    @Autowired
    private FastFileStorageClient ffsc;

    @RequestMapping(value = "/createFile")
    public String createFile(MultipartFile file) throws Exception{
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getInputStream());
        System.out.println(file.getSize());

        String fileExtName = FilenameUtils.getExtension(file.getOriginalFilename());

        // 文件流，文件大小，文件后缀
        StorePath storePath = ffsc.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), fileExtName, null);

        // 最终上传的路径
        System.out.println(storePath.getFullPath());

        // 返回文件的路径
        return fdfsServer+storePath.getFullPath();
    }

}
